//index.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')
const recordService = require('../../services/recordService.js')
const sharedService = require('../../services/sharedService.js')
//获取应用实例
const app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    record: undefined,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    // experienceScrollPercent: <0 - 100>
    experienceScrollPercent: 80,
    serviceImg: {}
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  loadUserInfo: function() {
    if (this.data.logged) return
    util.showBusy('正在登录')
    var that = this
    // 调用登录接口
    qcloud.login({
      success(result) {
        if (result) {
          util.showSuccess('登录成功')
          that.setData({
            userInfo: result,
            logged: true
          }, function() {
            that.getUserRecord()
          })
          getApp().globalData.userInfo = result
          that.getUserRecord()
        } else {
          // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
          qcloud.request({
            url: config.service.requestUrl,
            login: true,
            success(result) {
              util.showSuccess('登录成功')
              that.setData({
                userInfo: result.data.data,
                logged: true
              }, function () {
                that.getUserRecord()
              })
              getApp().globalData.userInfo = result.data.data
            },
            fail(error) {
              util.showModel('请求失败', error)
              console.log('request fail', error)
            }
          })
        }
      },

      fail(error) {
        util.showModel('登录失败', error)
        console.log('登录失败', error)
      }
    })
  },
  getUserRecord: function() {
    const userInfo = this.data.userInfo
    const that = this
    recordService.get(function(record) {
      if (record) {
        that.setData({ record })
      }
    })
  },
  onShareAppMessage: function(options){
    const that = this
    return sharedService.onShareAppMessage(options, function() {
      that.getUserRecord()
    });
  },
  onLoad: function () {
    const globalData = getApp().globalData
    const userInfo = globalData.userInfo
    const that = this
    if (userInfo) {
      // Re init record
      this.setData({ userInfo }, function () {
        that.getUserRecord()
      })
    } else {
      that.loadUserInfo()
    }
  },
  // 客服动画
  onShow: function () {
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: '50% 50%',
      timingFunction: 'ease'
    })

    this.animation = animation;
    this.setData({
      serviceImg: animation.export()
    })

    var angle = 0;
    var direction = true;
    //连续动画需要添加定时器,所传参数每次+1就行
    setInterval(function () {
      if (direction) {
        angle = angle + 1;
        angle > 0 ? direction = false : direction = true;
      } else {
        angle = angle - 1;
        angle < 0 ? direction = true : direction = false;
      }
      this.animation.rotate(20 * (angle)).step();
      this.setData({
        serviceImg: this.animation.export()
      })
    }.bind(this), 600);
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  toCount: function(){
    wx.navigateTo({
      url: '../math/math',
    })
  },
  toMultiply: function () {
    wx.navigateTo({
      url: '../multiply/multiply',
    })
  },
  toChallenge: function(){
    wx.navigateTo({
      url: '../challenge/challenge',
    })
  },
  totest: function (){
    wx.redirectTo({
      url: '../result/result?qn=30&cn=20&en=10&ac=67',
    })
  }
})
